import os
import time
import re
from urllib import parse
import json
from datetime import datetime
from scrapy.linkextractors import LinkExtractor
from scrapy.spiders import CrawlSpider, Rule
from selenium import webdriver

class LagouSpider(CrawlSpider):
    name = "lagou"
    allowed_domains = ["www.lagou.com"]
    start_urls = ["https://www.lagou.com"]

    rules = (Rule(LinkExtractor(allow=r"Items/"), callback="parse_item", follow=True),)


    def start_requests(self):
        # 配置启动控制浏览器
        """
        1.启动chrome浏览器[把chrome浏览器做成一个服务]
        chrome.exe --remote-debugging-port=9222
        注释：需要关闭所有的chrome浏览器实例，
        验证是否启动成功：http://localhost:9222/json
        2.scrapy链接到chrome浏览器
        """
        from selenium.webdriver.chrome.service import Service
        from selenium.webdriver.chrome.options import Options
        from selenium.webdriver.common.keys import Keys
        from selenium.webdriver.common.by import By

        chrome_options = Options()
        # chrome_options.add_argument("--headless")  # 如果需要无界面运行，可以添加这个参数
        chrome_options.add_argument("--disable-extensions")
        chrome_options.add_experimental_option("debuggerAddress", "127.0.0.1:9222")
        chromedriver_path = os.path.abspath("chromedriver/chromedriver.exe")
        service = Service(chromedriver_path)
        browser = webdriver.Chrome(service=service, options=chrome_options)
        # # 进入对应的网站
        browser.get("https://passport.lagou.com/login/login.html")
        try:
            browser.maximize_window()
        except Exception as e:
            pass
        
        username = "920919805@qq.com"
        password = "opq8225818.123"
        username_input = browser.find_element(By.CLASS_NAME, "iEUASn")
        password_input = browser.find_element(By.CLASS_NAME, "kwdeWU")

        # 清空并输入账号
        username_input.send_keys(Keys.CONTROL + "a")
        username_input.send_keys(username)

        # 清空并输入密码
        password_input.send_keys(Keys.CONTROL + "a")
        password_input.send_keys(password)

        # 用户协议
        protocol_link = browser.find_element(By.CSS_SELECTOR, "div.sc-furwcr")
        protocol_link.click()
        # 点击登录按钮
        login_button = browser.find_element(By.CSS_SELECTOR, "div.hmmuaS button")
        login_button.click()


        pass

    def parse_start_url(self, response):
        # 解析start_url
        pass

    def process_results(self, response):
        pass

    def parse_item(self, response):
        item = {}
        #item["domain_id"] = response.xpath('//input[@id="sid"]/@value').get()
        #item["name"] = response.xpath('//div[@id="name"]').get()
        #item["description"] = response.xpath('//div[@id="description"]').get()
        return item
